package com.hdu.other.tanxin;

import java.util.Comparator;
import java.util.PriorityQueue;

public class lc502 {
    class Solution {
        public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
            PriorityQueue<Project> lock = new PriorityQueue<>(Comparator.comparingInt(a -> a.capital));
            PriorityQueue<Project> unlock = new PriorityQueue<>(Comparator.comparingInt(a -> -a.profit));
            for (int i = 0; i < profits.length; i++) {
                lock.offer(
                        new Project(capital[i], profits[i])
                );
            }
            while (k-- > 0)
            {
                while (!lock.isEmpty() && lock.peek().capital <= w) {
                    unlock.offer(lock.poll());
                }
                if (!unlock.isEmpty()) {
                    w += unlock.poll().profit;
                }
            }
            return w;
        }

        class Project{

            int capital;
            int profit;

            public Project(int capital, int profit)
            {
                this.capital = capital;
                this.profit = profit;
            }
        }
    }
}
